package com.yoomiito.app.ui.my.psw;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseLazyFragmentPresenter;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.utils.YMToast;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;

public class VerifyCodePresenter extends BaseLazyFragmentPresenter<VerifyCodeFragment> {

    public VerifyCodePresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void sendSms(){
        YmApi.getYmService()
                .withdrawSms()
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        YMToast.showText("发送成功");
                        getV().sendSms();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                        YMToast.showText("发送失败");
                    }
                });
    }

    public void verifySms(String code){
        YmApi.getYmService()
                .verifyWithdrawSms(code)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        getV().verifySmsCode();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                    }
                });
    }
}
